package com.open.system.controller;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.open.system.domain.LoanSignDO;
import com.open.system.service.LoanSignService;
import com.alibaba.fastjson.JSONObject;
import com.open.common.utils.PageUtils;
import com.open.common.utils.Query;
import com.open.common.utils.R;
import com.open.loan.utils.HttpUtil;

/**
 * 签名
 * 
 * @author aaa
 * @email aaax
 * @date 2019-05-22 12:08:48
 */
 
@Controller
@RequestMapping("/system/loanSign")
public class LoanSignController {
	@Autowired
	private LoanSignService loanSignService;
	
	@GetMapping()
	@RequiresPermissions("system:loanSign:loanSign")
	String LoanSign(){
	    return "system/loanSign/loanSign";
	}
	
	@ResponseBody
	@GetMapping("/list")
	@RequiresPermissions("system:loanSign:loanSign")
	public PageUtils list(@RequestParam Map<String, Object> params){
		//查询列表数据
        Query query = new Query(params);
		List<LoanSignDO> loanSignList = loanSignService.list(query);
		int total = loanSignService.count(query);
		PageUtils pageUtils = new PageUtils(loanSignList, total);
		return pageUtils;
	}
	
	@GetMapping("/add")
	@RequiresPermissions("system:loanSign:add")
	String add(){
	    return "system/loanSign/add";
	}

	@GetMapping("/edit/{id}")
	@RequiresPermissions("system:loanSign:edit")
	String edit(@PathVariable("id") Long id,Model model){
		LoanSignDO loanSign = loanSignService.get(id);
		model.addAttribute("loanSign", loanSign);
	    return "system/loanSign/edit";
	}
	
	/**
	 * 保存
	 */
	@ResponseBody
	@PostMapping("/save")
	@RequiresPermissions("system:loanSign:add")
	public R save( LoanSignDO loanSign){
		if(StringUtils.isEmpty(loanSign.getSignName())){
			return R.error("签名不能为空");
		}
		if(StringUtils.isEmpty(loanSign.getRemark1())){
			return R.error("签名场景不能为空");
		}
		loanSign.setCreateTime(new Date());
		String accesskey = "QX7R54xfCtSY7bMx"; //用户开发key
        String accessSecret = "UQ0CCsD2zT1q0EWgryoy83n7R8gXJyvc"; //用户开发秘钥
		Map<String, String> parameters=new HashMap<String, String>();
		parameters.put("accesskey", accesskey);
		parameters.put("secret", accessSecret);
		parameters.put("sign", "【"+loanSign.getSignName()+"】");
		parameters.put("description",loanSign.getRemark1());
		String returnBack=HttpUtil.sendGet("http://api.1cloudsp.com/open/api/addSign", parameters);
		System.out.println(returnBack);
		JSONObject returnBackJson =JSONObject.parseObject(returnBack);
		if(0==Integer.parseInt(String.valueOf(returnBackJson.get("ret")))){
			String signIdStr=String.valueOf(returnBackJson.get("data"));
			JSONObject signIdJson =JSONObject.parseObject(signIdStr);
			String signId=String.valueOf(signIdJson.get("signId"));
			System.out.println(signId);
			loanSign.setSignId(Integer.parseInt(signId));
			loanSign.setStatus("0");
		}else {
			return R.error();
		}
		if(loanSignService.save(loanSign)>0){
			return R.ok();
		}
		return R.error();
	}
	/**
	 * 修改
	 */
	@ResponseBody
	@RequestMapping("/update")
	@RequiresPermissions("system:loanSign:edit")
	public R update( LoanSignDO loanSign){
		loanSignService.update(loanSign);
		return R.ok();
	}
	
	/**
	 * 删除
	 */
	@PostMapping( "/remove")
	@ResponseBody
	@RequiresPermissions("system:loanSign:remove")
	public R remove( Long id){
		if(loanSignService.remove(id)>0){
		return R.ok();
		}
		return R.error();
	}
	
	/**
	 * 删除
	 */
	@PostMapping( "/batchRemove")
	@ResponseBody
	@RequiresPermissions("system:loanSign:batchRemove")
	public R remove(@RequestParam("ids[]") Long[] ids){
		loanSignService.batchRemove(ids);
		return R.ok();
	}
	
}
